package com.jxs.android.cmcc.db;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.jxs.android.cmcc.boot.SelfApplication;

public class KipConfigHolder {

	static public interface Table {
		public static final String TABLE_NAME = "system_kip_config_tab";
		public static final String KIP_ID = "kip_id";
		public static final String KIP_STATE_DATE = "kip_state_date";
		public static final String[] COLUMNS = new String[] { KIP_ID, KIP_STATE_DATE };
	}

	static public enum Scope {
		ALL, // 0全部指标，
		SELECTED, // 1SE已选择指标，
		UN_SELECTED;// 2未选择指标
	}

	public static void creater(SQLiteDatabase db) {
		StringBuffer createSQL = new StringBuffer(" CREATE TABLE IF NOT EXISTS ");
		createSQL.append(Table.TABLE_NAME);
		createSQL.append(" (_id INTEGER PRIMARY KEY AUTOINCREMENT, ");
		createSQL.append(Table.KIP_ID + " varchar(20), ");
		createSQL.append(Table.KIP_STATE_DATE + " text ");
		createSQL.append(" )");
		db.execSQL(createSQL.toString());
	}

	public static void saveOrUpdate(String key, String value) {
		ContentResolver contentResolver = SelfApplication.getApplication().getContentResolver();
		Cursor cursor = null;
		cursor = contentResolver.query(KipConfigProvider.CONTENT_URI, Table.COLUMNS, Table.KIP_ID + "=? ", new String[] { key }, null);
		ContentValues values = new ContentValues();
		values.put(Table.KIP_ID, key);
		values.put(Table.KIP_STATE_DATE, value);

		if (null != cursor && cursor.getCount() == 0) {
			contentResolver.insert(KipConfigProvider.CONTENT_URI, values);
		} else {
			contentResolver.update(KipConfigProvider.CONTENT_URI, values, Table.KIP_ID + "=? ", new String[] { key });
		}
		if (null != cursor) {
			cursor.close();
		}
	}

	public static String getValue(String key) {
		String value = null;
		ContentResolver contentResolver = SelfApplication.getApplication().getContentResolver();
		Cursor cursor = null;
		cursor = contentResolver.query(KipConfigProvider.CONTENT_URI, Table.COLUMNS, Table.KIP_ID + "=? ", new String[] { key }, null);
		if (null != cursor && cursor.moveToFirst()) {
			value = cursor.getString(cursor.getColumnIndex(Table.KIP_STATE_DATE));
		}
		if (null != cursor) {
			cursor.close();
		}
		return value;
	}

	public static String getDefualValue() {
		String value = null;
		ContentResolver contentResolver = SelfApplication.getApplication().getContentResolver();
		Cursor cursor = null;
		cursor = contentResolver.query(KipConfigProvider.CONTENT_URI, Table.COLUMNS, null, null, null);
		if (null != cursor && cursor.moveToFirst()) {
			value = cursor.getString(cursor.getColumnIndex(Table.KIP_STATE_DATE));
		}
		if (null != cursor) {
			cursor.close();
		}
		return value;
	}
}
